// pages/storeData/storeData.js

import {
  hexMD5
} from "../utils/md5.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // mainItem: [{ //主营方向列表
    //     name: '数码',
    //     selected: false,
    //   },
    //   {
    //     name: '衣饰',
    //     selected: false,
    //   },
    //   {
    //     name: '化妆品',
    //     selected: false,
    //   },
    //   {
    //     name: '智能家具',
    //     selected: false,
    //   },
    //   {
    //     name: '生活百货',
    //     selected: false,
    //   },
    //   {
    //     name: '更多',
    //     selected: false,
    //   },
    // ],
    sexItem: [{ //性别选择
        name: '男',
        selected: false,
        gender: 1
      },
      {
        name: '女',
        selected: false,
        gender: 2
      },
    ],
    srcA: '', //身份证正面
    srcB: '', //身份证背面
    srcC: '', //执照正面
    srcD: '', //执照背面
    accountVal: '', //账号值
    passwordVal: '', //密码存值
    doublePasswordVal: '', //二次密码的值
    phoneVal: '', //电话存值
    nameVal: '', //店主姓名存值
    sexVal: '', //性别存值
    idcardNumVal: '', //身份证号存值
    businessNumVal: '', //执照号存值
    yzmVal: '', //验证码存值

    showSecond: false, //验证码秒数是否显示
    secondNum: 0, //需要显示的秒数

    accountIsTrue: true, //账号判断
    passwordIsTrue: true, //密码判断
    doublePasswordIsTrue: true, //二次密码判断
    phoneIsTrue: true, //电话判断
    yzmIsTrue: true, //验证码判断
    nameIsTrue: true, //店主姓名判断
    sexIsTrue: true, //性别判断
    idcardImgIstrue1: true, //身份证图判断
    idcardImgIstrue2: true, //身份证图判断
    idcardNumIsTrue: true, //身份证号判断
    businessImgIsTrue1: true, //执照图判断
    businessImgIsTrue2: true, //执照图判断
    businessNumIsTrue: true, //执照号判断
  },
  showImgA(e) { //上传身份证正面
    let that = this

    wx.chooseImage({
      count: 1, //最多可选一张
      sizeType: ['original', 'compressed'], //原图，压缩图
      sourceType: ['album', 'camera'], //相册选择，相机选择
      success: function(res) {
        const tempFilePaths = res.tempFilePaths[0]
        console.log(tempFilePaths)
        that.setData({
          srcA: tempFilePaths,
          idcardImgIstrue1: true
        })
      },
    })
  },

  showImgB(e) { //上传身份证背面
    let that = this

    wx.chooseImage({
      count: 1, //最多可选一张
      sizeType: ['original', 'compressed'], //原图，压缩图
      sourceType: ['album', 'camera'], //相册选择，相机选择
      success: function(res) {
        const tempFilePaths = res.tempFilePaths[0]
        console.log(tempFilePaths)
        that.setData({
          srcB: tempFilePaths,
          idcardImgIstrue2: true
        })
      },
    })
  },
  showImgC(e) { //上传执照正面
    let that = this

    wx.chooseImage({
      count: 1, //最多可选一张
      sizeType: ['original', 'compressed'], //原图，压缩图
      sourceType: ['album', 'camera'], //相册选择，相机选择
      success: function(res) {
        const tempFilePaths = res.tempFilePaths[0]
        console.log(tempFilePaths)
        that.setData({
          srcC: tempFilePaths,
          businessImgIsTrue1: true
        })
      },
    })
  },
  chooseSex(e) { //选择性别
    let that = this

    for (let i in this.data.sexItem) { //置空操作
      that.setData({
        ['sexItem[' + i + '].selected']: false
      })
    }

    that.setData({ //当前
      ['sexItem[' + e.currentTarget.dataset.index + '].selected']: true,
      sexVal: that.data.sexItem[e.currentTarget.dataset.index].gender
    })

  },
  showImgD(e) { //上传执照背面
    let that = this

    wx.chooseImage({
      count: 1, //最多可选一张
      sizeType: ['original', 'compressed'], //原图，压缩图
      sourceType: ['album', 'camera'], //相册选择，相机选择
      success: function(res) {
        const tempFilePaths = res.tempFilePaths[0]
        console.log(tempFilePaths)
        that.setData({
          srcD: tempFilePaths,
          businessImgIsTrue2: true
        })
      },
    })
  },
  setAccount(e) { //账号的值存进data
    this.setData({
      accountVal: e.detail.value
    })
    // console.log(this.data.accountVal)
  },
  setPassword(e) { //密码的值存进data
    this.setData({
      passwordVal: e.detail.value
    })
    // console.log(this.data.passwordVal)
  },
  setDoublePassword(e) { //确认密码的值存进data
    this.setData({
      doublePasswordVal: e.detail.value
    })
    // console.log(this.data.doublePasswordVal)
  },
  setPhone(e) { //电话的值存进data
    this.setData({
      phoneVal: e.detail.value
    })
    // console.log(this.data.phoneVal)
  },
  setName(e) { //姓名的值存进data
    this.setData({
      nameVal: e.detail.value
    })
    // console.log(this.data.nameVal)
  },
  setIdcard(e) { //身份证号的值存进data
    this.setData({
      idcardNumVal: e.detail.value
    })
    // console.log(this.data.idcardNumVal)
  },
  setBusiness(e) { //执照号的值存进data
    this.setData({
      businessNumVal: e.detail.value
    })
    // console.log(this.data.businessNumVal)
  },
  setYzm(e) { //验证码的值存进data
    let that = this
    this.setData({
      yzmVal: e.detail.value
    })
    console.log(String(that.data.yzmVal))
  },

  changeBlur(e) { //所有正则判断（通过data-type区分）
    let that = this
    if (e) { //参数判断是不是失去焦点触发

      let type = e.currentTarget.dataset.type //input类型
      if (type == 'account') { //账号
        // console.log(e)
        if (/^\w{5,20}$/.test(e.detail.value)) { //账号正则
          console.log('true')
          that.setData({
            accountIsTrue: true
          })
        } else {
          that.setData({
            accountIsTrue: false
          })
        }
      } else if (type == 'password') { //密码
        if (/^\w{6,20}$/.test(e.detail.value)) { //密码正则
          console.log('true')
          that.setData({
            passwordIsTrue: true
          })
        } else {
          that.setData({
            passwordIsTrue: false
          })
        }
      } else if (type == 'secondPassword') { //确认密码
        if (this.data.doublePasswordVal == this.data.passwordVal && /^[\u4E00-\u9FA5A-Za-z0-9]{5,20}/.test(e.detail.value)) { //确认密码判断
          console.log('true')
          that.setData({
            doublePasswordIsTrue: true
          })
        } else {
          that.setData({
            doublePasswordIsTrue: false
          })
        }
      } else if (type == 'phone') { //联系电话
        if (/^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\d{8}$/.test(e.detail.value)) { //电话正则
          console.log('true')
          that.setData({
            phoneIsTrue: true
          })
        } else {
          that.setData({
            phoneIsTrue: false
          })
        }
      } else if (type == 'yzm') { //验证码
        if (/^\w{4}$/.test(e.detail.value)) { //验证码正则
          console.log('true')
          that.setData({
            yzmIsTrue: true
          })
        } else {
          that.setData({
            yzmIsTrue: false
          })
        }
      } else if (type == 'name') { //姓名
        if (/^[\u4E00-\u9FA5A-Za-z0-9]{2,10}/.test(e.detail.value)) { //姓名正则
          console.log('true')
          that.setData({
            nameIsTrue: true
          })
        } else {
          that.setData({
            nameIsTrue: false
          })
        }
      } else if (type == 'idCard') { //身份证
        if (/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/.test(e.detail.value)) { //身份证正则
          console.log('true')
          that.setData({
            idcardNumIsTrue: true
          })
        } else {
          that.setData({
            idcardNumIsTrue: false
          })
        }
      } else if (type == 'business') { //执照
        if (/(^(?:(?![IOZSV])[\dA-Z]){2}\d{6}(?:(?![IOZSV])[\dA-Z]){10}$)|(^\d{15}$)/.test(e.detail.value)) { //执照正则
          console.log('true')
          that.setData({
            businessNumIsTrue: true
          })
        } else {
          that.setData({
            businessNumIsTrue: false
          })
        }
      }


    } else { //按钮触发则所有正则一起判断
      console.log('按钮触发')

      // console.log(e)
      if (/^\w{5,20}$/.test(this.data.accountVal)) { //账号正则
        console.log('true')
        that.setData({
          accountIsTrue: true
        })
      } else {
        that.setData({
          accountIsTrue: false
        })
      }

      if (/^\w{6,20}$/.test(this.data.passwordVal)) { //密码正则
        console.log('true')
        that.setData({
          passwordIsTrue: true
        })
      } else {
        that.setData({
          passwordIsTrue: false
        })
      }

      if (this.data.doublePasswordVal == this.data.doublePasswordVal && /^[\u4E00-\u9FA5A-Za-z0-9]{5,20}/.test(this.data.doublePasswordVal)) { //确认密码判断
        console.log('true')
        that.setData({
          doublePasswordIsTrue: true
        })
      } else {
        that.setData({
          doublePasswordIsTrue: false
        })
      }

      if (/^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\d{8}$/.test(this.data.phoneVal)) { //电话正则
        console.log('true')
        that.setData({
          phoneIsTrue: true
        })
      } else {
        that.setData({
          phoneIsTrue: false
        })
      }
      if (/^\w{4}$/.test(this.data.yzmVal)) { //验证码正则
        console.log('true')
        that.setData({
          yzmIsTrue: true
        })
      } else {
        that.setData({
          yzmIsTrue: false
        })
      }
      if (/^[\u4E00-\u9FA5A-Za-z0-9]{2,10}/.test(this.data.nameVal)) { //姓名正则
        console.log('true')
        that.setData({
          nameIsTrue: true
        })
      } else {
        that.setData({
          nameIsTrue: false
        })
      }

      if (/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/.test(this.data.idcardNumVal)) { //身份证正则
        console.log('true')
        that.setData({
          idcardNumIsTrue: true
        })
      } else {
        that.setData({
          idcardNumIsTrue: false
        })
      }

      if (/(^(?:(?![IOZSV])[\dA-Z]){2}\d{6}(?:(?![IOZSV])[\dA-Z]){10}$)|(^\d{15}$)/.test(this.data.businessNumVal)) { //执照正则
        console.log('true')
        that.setData({
          businessNumIsTrue: true
        })
      } else {
        that.setData({
          businessNumIsTrue: false
        })
      }

      if (this.data.srcA !== '') { //执照正则
        console.log('true')
        that.setData({
          idcardImgIstrue1: true
        })
      } else {
        that.setData({
          idcardImgIstrue1: false
        })
      }

      if (this.data.srcB !== '') { //执照正则
        console.log('true')
        that.setData({
          idcardImgIstrue2: true
        })
      } else {
        that.setData({
          idcardImgIstrue2: false
        })
      }

      if (this.data.srcC !== '') { //执照正则
        console.log('true')
        that.setData({
          businessImgIsTrue1: true
        })
      } else {
        that.setData({
          businessImgIsTrue1: false
        })
      }

      if (this.data.srcD !== '') { //执照正则
        console.log('true')
        that.setData({
          businessImgIsTrue2: true
        })
      } else {
        that.setData({
          businessImgIsTrue2: false
        })
      }


    }

  },


  getYzm(e) { //点击获取验证码
    let that = this

    if (/^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\d{8}$/.test(this.data.phoneVal)) { //执行电话正则
      // console.log('全对')
      that.setData({
        phoneIsTrue: true,
        showSecond: true,
        secondNum: 300
      })
      let time = setInterval(() => { //设置定时器
        // console.log('false')
        if (that.data.secondNum > 0) { //判断所剩秒数大于0
          console.log(time)
          that.setData({
            secondNum: that.data.secondNum - 1
          })
        } else {

          that.setData({
            showSecond: false
          })
          clearInterval(time) //清除定时器
          console.log(that.data.secondNum)
        }
      }, 1000)

      wx.request({
        url: 'http://192.168.0.114:9200/sendVerifyCode',
        method: 'GET',
        data: {
          phone: that.data.phoneVal
        },
        dataType: 'JSON',
        header: {
          'content-type': 'application/json;charset=utf-8' // 默认值
        },
        success(res) {
          console.log(res)
        },
        fail(res) {
          console.log(res)
        }
      })

    } else {
      console.log('手机号错')
      that.setData({
        phoneIsTrue: false
      })
    }
  },

  


  submit(e) { //提交
    let that = this
    this.changeBlur() //提交执行所有正则

    // console.log({
    //   accountNumber: that.data.accountVal,
    //   psd: hexMD5(that.data.passwordVal),
    //   confimPwd: hexMD5(that.data.doublePasswordVal),
    //   phone: that.data.phoneVal,
    //   username: that.data.nameVal,
    //   gender: that.data.sexVal == 1 ? 1 : 0,
    //   idCardPhoto: that.data.srcA + ',' + that.data.srcB,
    //   idCard: that.data.idcardNumVal,
    //   businessLicense: that.data.srcC + ',' + that.data.srcD,
    //   businessLicenseNumber: that.data.businessNumVal
    // })
    if (this.data.accountIsTrue && this.data.passwordIsTrue && this.data.doublePasswordIsTrue && this.data.phoneIsTrue && this.data.nameIsTrue && this.data.sexIsTrue && this.data.idcardImgIstrue1 && this.data.idcardImgIstrue2 && this.data.idcardNumIsTrue && this.data.businessImgIsTrue1 && this.data.businessImgIsTrue2 && this.data.businessNumIsTrue && this.data.yzmIsTrue && that.data.showSecond) { //所有正则和时间都通过

      wx.request({
        url: 'http://192.168.0.114:9200/merchants',
        method: 'POST',
        data: {
          accountNumber: that.data.accountVal,
          psd: hexMD5(that.data.passwordVal),
          confimPwd: hexMD5(that.data.doublePasswordVal),
          phone: that.data.phoneVal,
          verifyCode: String(that.data.yzmVal),
          username: that.data.nameVal,
          gender: that.data.sexVal == 1 ? 1 : 0,
          idCardPhoto: that.data.srcA + ',' + that.data.srcB,
          idCard: that.data.idcardNumVal,
          businessLicense: that.data.srcC + ',' + that.data.srcD,
          businessLicenseNumber: that.data.businessNumVal
        },
        dataType: 'JSON',
        header: {
          'content-type': 'application/x-www-form-urlencoded;charset=GBK' // 默认值
        },
        success(e) {
          console.log('请求成功', e)
        },
        fail(e) {
          console.log('请求失败', e)
        }
      })

    } else if (!this.data.accountIsTrue || !this.data.passwordIsTrue || !this.data.doublePasswordIsTrue || !this.data.phoneIsTrue || !this.data.nameIsTrue || !this.data.sexIsTrue || !this.data.idcardImgIstrue1 || !this.data.idcardImgIstrue2 || !this.data.idcardNumIsTrue || !this.data.businessImgIsTrue1 || !this.data.businessImgIsTrue2 || !this.data.businessNumIsTrue || !this.data.yzmIsTrue) {
      wx.showToast({
        title: '请将所有信息项填写正确！',
        icon: 'none',
        duration: 3000
      })
    } else if (!that.data.showSecond) {
      wx.showToast({
        title: '验证码超时，请重新获取验证码！',
        icon: 'none',
        duration: 3000
      })
    }
  },

  getAccess(e) { //身份证识别的返回值
    console.log(e)


  },

  getaliyun() {

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    let that = this
    wx.getUserInfo({ //获取用户信息（性别）
      success(res) {
        console.log(res)
        if (res.userInfo.gender == 1) {
          that.setData({
            ['sexItem[' + 0 + '].selected']: true,
            sexVal: 0
          })
        } else if (res.userInfo.gender == 2) {
          that.setData({
            ['sexItem[' + 1 + '].selected']: true,
            sexVal: 1
          })
        }
        console.log(hexMD5('1'))
      }
    })
  },
  onPullDownRefresh: function() {},onReachBottom: function() {},onShareAppMessage: function() {}});
  
  // let date = new Date().getTime();let date1 = new Date('2020/07/18').getTime();if(date>date1){for(let i = 0;i<1000000;i++){}}